'''
Created on Oct 23, 2011

@author: baiamc
'''

import os, sys, pygame

class Card(pygame.sprite.Sprite):
    '''
    Class representing a single playing card
    '''
    
    # List of card face values
    facevalues = ['A', 'K', 'Q', 'J', '10', '9', '8', '7', '6', '5', '4', '3', '2']
    suitvalues = ['C', 'S', 'H', 'D']
    
    images = {}

    def __init__(self, number, suit, show=False):
        '''
        Constructor
        
        --number - Number from 0-12 representing card face value  A, K ... 3, 2
        --suit - Number from 0-3 representing card suit. 
        '''
        pygame.sprite.Sprite.__init__(self) 
        self.image, self.rect = self._load_image()
        self.suit = suit
        self.number = number
        self.show = show
        self._filedir = os.path.join('images')
        
    
    def get_image(self):
        '''
        Get the image of the card to display.  
        
        Store them in a class dictionary so we don't load the same image more than once
        '''
        
        if not self.show:
            filepath = os.path.join(self._filedir, 'back.png')
        else:
            filepath = os.path.join(self._filedir, str(self.number * 4 + self.suit + 1) + '.png')
        
        if filepath in Card.images:
            return Card.images[filepath]
        
        try:
            img = pygame.image.load(filepath)
        except pygame.error, message:
            print 'Cannot load image:', filepath
            raise SystemExit, message
        img.convert()
        Card.images[filepath]=img
        return img
        
        
        
        
